﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Social.Facebook;
using Social.YouTube;
using Social.FlickrApi;
using Logic.SocialNetworks;
using SocialNetworks.Config;

public partial class Auth : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		string from = Request.QueryString["from"];
		bool success = false;
    	switch (from)
    	{
			//case "facebook":
			//    var fbApi = new FacebookRest(new FacebookConfig());
			//    fbApi.Authenticate();

			//    CloseWindow();
			//    break;

    		case "flickr":
				// Get FROB and user TOKEN
    			var api = new FlickrApi(new FlickrConfig());
    			api.Authenticate();
    			break;

			case "youtube":
				var youTubeApi = new YouTubeApi(new YouTubeConfig());
				youTubeApi.Authenticate();				
				success = youTubeApi.IsAuthenticated;
    			break;
			case "graph":
				var config = new FbGraphConfig();
				var graphApi = new Social.Facebook.FacebookGraph(config);
				graphApi.Authenticate();
				success = graphApi.IsAuthenticated;
				break;
			case "twitter":
				var TWApi = new Social.Twitter.TwitterAPI(new TwitterConfig());
				TWApi.Authenticate();
				success = TWApi.IsAuthenticated;
				break;
			default:
    			break;
    	}
		CloseWindow(from, success);		
    }

	private void CloseWindow(string from, bool success)
	{
		string script = string.Format("window.opener.ChangeSnDisplay('{0}',{1}); self.close();", from, success?"true":"false");
		Page.ClientScript.RegisterStartupScript(GetType(), "close", script, true);
	}

	
}